home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Core / ValueNS.cpp < prev   
Encoding:
C/C++ Source or Header  |  1996-08-28  |  15.7 KB  |  602 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        ValueNS.cpp
  3.  
  4.     Contains:    Implementation of ODValueNameSpace class
  5.  
  6.     Owned by:    Reggie Adkins
  7.  
  8.     Copyright:    © 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <4>      6/5/96    TJ        1330687: Now correctly pass buffer ptr by
  13.                                     value in WriteSizeAndBytes
  14.          <3>     5/24/96    jpa        1330687: Fixed parameter ordering for
  15.                                     PlatformFile::Write calls.
  16.          <2>     5/23/96    RA        1330687: ValueNS should use
  17.                                     platformFile->write instead of FSWrite
  18.  
  19.     To Do:
  20.         Get prefs file name from a string resource.
  21.     In Progress:
  22.         
  23. */
  24.  
  25. #ifndef _PLFMDEF_
  26. #include "PlfmDef.h"
  27. #endif
  28.  
  29. #ifndef _EXCEPT_
  30. #include "Except.h"
  31. #endif
  32.  
  33. #define ODValueNameSpace_Class_Source
  34. #include <ValueNS.xih>
  35.  
  36. #ifndef SOM_ODNameSpace_xh
  37. #include "NamSpac.xh"
  38. #endif
  39.  
  40. #ifndef _STRHSHTB_
  41. #include "StrHshTb.h"
  42. #endif
  43.  
  44. #ifndef _PASCLSTR_
  45. #include "PasclStr.h"
  46. #endif
  47.  
  48. #ifndef __STRING__
  49. #include <string.h>
  50. #endif
  51.  
  52. #ifndef __LIMITS__
  53. #include <limits.h>
  54. #endif
  55.  
  56. #ifndef __LOWMEM__
  57. #include <LowMem.h>
  58. #endif
  59.  
  60. #ifndef _ODTYPES_
  61. #include <ODTypes.h>
  62. #endif
  63.  
  64. #ifndef SOM_ODStorageUnitView_xh
  65. #include <SUView.xh>
  66. #endif
  67.  
  68. #ifndef _ODMEMORY_
  69. #include "ODMemory.h"
  70. #endif
  71.  
  72. #ifndef _ISOSTR_
  73. #include "ISOStr.h"
  74. #endif
  75.  
  76. #ifndef SOM_ODValueIterator_xh
  77. #include <ValueItr.xh>
  78. #endif
  79.  
  80. #ifndef _BNDNSUTL_
  81. #include "BndNSUtl.h"
  82. #endif
  83.  
  84. #ifndef _PLFMFILE_
  85. #include "PLFMFile.h"
  86. #endif
  87.  
  88. #ifndef _STORUTIL_
  89. #include <StorUtil.h>
  90. #endif
  91.  
  92. #ifndef _BARRAY_
  93. #include <BArray.h>
  94. #endif
  95.  
  96. #ifndef _TEMPOBJ_
  97. #include <TempObj.h>
  98. #endif
  99.  
  100. #ifndef _UTILERRS_
  101. #include <UtilErrs.h>
  102. #endif
  103.  
  104. #ifndef SOM_ODNameSpaceManager_xh
  105. #include "NmSpcMg.xh"
  106. #endif
  107.  
  108. #pragma segment ODValueNameSpace
  109.  
  110. //==============================================================================
  111. // Implementation notes
  112. //==============================================================================
  113.  
  114. /*
  115. Hash tables used for the implementation are lazily instantiated. They will be
  116. created at the first request to add an entry.
  117. */
  118.  
  119. //==============================================================================
  120. // Constants
  121. //==============================================================================
  122.  
  123. //==============================================================================
  124. // Function Prototype
  125. //==============================================================================
  126.  
  127. //==============================================================================
  128. // Local Functions
  129. //==============================================================================
  130.  
  131. //==============================================================================
  132. // ODValueNameSpace
  133. //==============================================================================
  134.  
  135. //------------------------------------------------------------------------------
  136. // ODValueNameSpace: InitValueNameSpace
  137. //------------------------------------------------------------------------------
  138.  
  139. SOM_Scope void  SOMLINK ODValueNameSpaceInitValueNameSpace(ODValueNameSpace *somSelf, Environment *ev,
  140.         ODNameSpaceManager*        mgr,
  141.         ODNameSpace*            parent,
  142.         ODULong                    numExpectedEntries,
  143.         ODISOStr                name)
  144. {
  145.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  146.     ODValueNameSpaceMethodDebug("ODValueNameSpace","InitValueNameSpace");
  147.  
  148.     SOM_TRY
  149.  
  150.     somSelf->SetType( ev, kODNSDataTypeODValue );
  151.     somSelf->InitNameSpace(ev, mgr, parent, numExpectedEntries, name);
  152.     
  153.     SOM_CATCH_ALL
  154.     SOM_ENDTRY
  155. }
  156.  
  157. //------------------------------------------------------------------------------
  158. // ODValueNameSpace: ~ODValueNameSpace
  159. //------------------------------------------------------------------------------
  160.  
  161. SOM_Scope void  SOMLINK ODValueNameSpacesomUninit(ODValueNameSpace *somSelf)
  162. {
  163.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  164.     ODValueNameSpaceMethodDebug("ODValueNameSpace","somUninit");
  165.  
  166.     Environment* ev = somGetGlobalEnvironment();
  167.     
  168.     SOM_TRY
  169.  
  170.     StringHashTable* hashTable = somSelf->GetHashTable(ev);
  171.     StringHashTableIterator* hashTableItr = new StringHashTableIterator(hashTable);
  172.  
  173.     if (hashTableItr)
  174.     {
  175.         ODULong    len;
  176.         ODUByte*    string;
  177.         ODPtr buffer = kODNULL;
  178.         ODULong size = 0;
  179.         for(hashTableItr->First(&string, &len ,&buffer, &size);
  180.             hashTableItr->IsNotComplete();
  181.             hashTableItr->Next(&string, &len ,&buffer, &size))
  182.         {
  183.             ODDisposePtr( buffer );
  184.         }
  185.         
  186.         delete ( hashTableItr );
  187.     }
  188.     
  189.     ODValueNameSpace_parent_ODNameSpace_somUninit(somSelf);
  190.  
  191.     SOM_CATCH_ALL
  192.     SOM_ENDTRY
  193. }
  194.  
  195. //------------------------------------------------------------------------------
  196. // ODValueNameSpace: Purge
  197. //------------------------------------------------------------------------------
  198.  
  199. SOM_Scope ODSize  SOMLINK ODValueNameSpacePurge(ODValueNameSpace *somSelf, Environment *ev,
  200.         ODSize size)
  201. {
  202.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  203.     ODValueNameSpaceMethodDebug("ODValueNameSpace","Purge");
  204.  
  205.     ODUnused(size);
  206.     return 0;
  207. }
  208.  
  209. //------------------------------------------------------------------------------
  210. // ODValueNameSpace: Register
  211. //------------------------------------------------------------------------------
  212.  
  213. SOM_Scope void  SOMLINK ODValueNameSpaceRegister(ODValueNameSpace *somSelf, Environment *ev,
  214.         ODISOStr key, ODByteArray* value)
  215. {
  216.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  217.     ODValueNameSpaceMethodDebug("ODValueNameSpace","Register");
  218.  
  219.     ODPtr buffer = kODNULL;
  220.     ODULong size = 0;
  221.     
  222.     SOM_TRY
  223.     
  224.     ODULong len = ODISOStrLength(key);
  225.     StringHashTable* hashTable = somSelf->GetHashTable(ev);
  226.     
  227.     if (hashTable->Find((ODUByte*)key, len, &buffer, &size))
  228.     {
  229.         // destroy pre-existing old value to prevent a memory leak
  230.         ODDisposePtr( buffer );
  231.         hashTable->Remove((ODUByte*)key, len);
  232.     }
  233.     
  234.     ODULong valueLength = value->_length;
  235.     buffer = ODNewPtrClear(valueLength, kDefaultHeapID);
  236.     ODBlockMove( value->_buffer, buffer, valueLength );
  237.     hashTable->Insert((ODUByte*)key,len, buffer, valueLength);
  238.  
  239.     if (somSelf->GetNameSpaceManager(ev)->
  240.                        GetMaxISOStrLength(ev) < valueLength)
  241.         somSelf->GetNameSpaceManager(ev)->
  242.                         SetMaxISOStrLength(ev, valueLength);
  243.     
  244.     SOM_CATCH_ALL
  245.     SOM_ENDTRY
  246. }
  247.  
  248. //------------------------------------------------------------------------------
  249. // ODValueNameSpace: Unregister
  250. //------------------------------------------------------------------------------
  251.  
  252. SOM_Scope void  SOMLINK ODValueNameSpaceUnregister(ODValueNameSpace *somSelf, Environment *ev,
  253.         ODISOStr key)
  254. {
  255.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  256.     ODValueNameSpaceMethodDebug("ODValueNameSpace","ODValueNameSpaceUnregister");
  257.  
  258.     ODPtr buffer = kODNULL;
  259.     ODULong size = 0;
  260.  
  261.     SOM_TRY
  262.  
  263.     size_t len = strlen(key);
  264.     somSelf->GetHashTable(ev)->Find((ODUByte*)key, len, &buffer, &size);
  265.     ODDisposePtr( buffer );
  266.     somSelf->GetHashTable(ev)->Remove((ODUByte*)key, len);
  267.     
  268.     SOM_CATCH_ALL
  269.     SOM_ENDTRY
  270. }
  271.  
  272. //------------------------------------------------------------------------------
  273. // ODValueNameSpace: GetEntry
  274. //------------------------------------------------------------------------------
  275.  
  276. SOM_Scope ODBoolean  SOMLINK ODValueNameSpaceGetEntry(ODValueNameSpace *somSelf, Environment *ev,
  277.         ODISOStr key, ODByteArray* value)
  278. {
  279.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  280.     ODValueNameSpaceMethodDebug("ODValueNameSpace","GetEntry");
  281.  
  282.     ODBoolean    result = kODFalse;
  283.     value->_buffer = kODNULL;
  284.     value->_maximum = value->_length = 0;
  285.  
  286.     SOM_TRY
  287.  
  288.     ODPtr buffer = kODNULL;
  289.     ODULong size = 0;
  290.  
  291.     if (somSelf->GetType(ev) == kODNSDataTypeODValue)
  292.     {
  293.         size_t len = strlen(key);
  294.         result = somSelf->GetHashTable(ev)->Find((ODUByte*)key, len, 
  295.                                                                 &buffer, &size);
  296.         if ( result == kODFalse )
  297.         {
  298.             ODValueNameSpace *parent =(ODValueNameSpace*)somSelf->GetParent(ev);
  299.             if (parent)
  300.             {
  301.                 if (parent->GetType(ev) == kODNSDataTypeODValue)
  302.                     result = parent->GetEntry(ev, key, value);
  303.             }
  304.         }
  305.         else
  306.         {
  307.             value->_length = value->_maximum = size;
  308.             if ( size > 0 )
  309.             {
  310.                 value->_buffer = (octet*) ODNewPtr(size, kDefaultHeapID);
  311.                 ODBlockMove( buffer, value->_buffer, size);
  312.             }
  313.         }
  314.     }
  315.     
  316.     SOM_CATCH_ALL
  317.     SOM_ENDTRY
  318.  
  319.     return result;
  320. }
  321.  
  322. //------------------------------------------------------------------------------
  323. // ODValueNameSpace: CreateIterator
  324. //------------------------------------------------------------------------------
  325.  
  326. SOM_Scope ODValueIterator*  SOMLINK ODValueNameSpaceCreateIterator(ODValueNameSpace *somSelf, Environment *ev)
  327. {
  328.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  329.     ODValueNameSpaceMethodDebug("ODValueNameSpace","CreateIterator");
  330.  
  331.     ODValueIterator* theValueIterator = kODNULL;
  332.     
  333.     SOM_TRY
  334.     
  335.     theValueIterator = new ODValueIterator;
  336.     THROW_IF_NULL(theValueIterator); // check all SOM new's for nil
  337.     theValueIterator->InitValueIterator(ev, somSelf);
  338.     
  339.     SOM_CATCH_ALL
  340.     SOM_ENDTRY
  341.  
  342.     return theValueIterator;
  343. }
  344.  
  345. //------------------------------------------------------------------------------
  346. // WriteULong
  347. //------------------------------------------------------------------------------
  348.  
  349. static void WriteULong(PlatformFile* file, ODStorageUnitView* view,
  350.                             ODULong value)
  351. {
  352.     ODSLong    length ;
  353.     Environment* ev = somGetGlobalEnvironment ();
  354.  
  355.     length = sizeof(value);
  356.     if (view) {
  357.         StorageUnitViewSetValue(view, ev, length, &value);
  358.     }
  359.     else
  360.     {
  361.         ODSLong savedLength = length;
  362.         file->Write(&value,&length);
  363.         if (savedLength != length)
  364.             THROW(kODErrWriteErr);
  365.     }
  366. }
  367.  
  368. //------------------------------------------------------------------------------
  369. // WriteSizeAndBytes
  370. //------------------------------------------------------------------------------
  371.  
  372. static void WriteSizeAndBytes(PlatformFile* file, ODStorageUnitView* view,
  373.                                 ODPtr bytes, ODULong length)
  374. {
  375.     Environment* ev = somGetGlobalEnvironment ();
  376.  
  377.     // WRITE LENGTH OF STREAM
  378.     WriteULong(file, view, length);
  379.  
  380.     // WRITE STREAM
  381.     if (view)
  382.         StorageUnitViewSetValue(view, ev, length, bytes);
  383.     else
  384.     {
  385.         ODSLong writtenLength = length;
  386.         file->Write(bytes,&writtenLength);
  387.         if (writtenLength != length)
  388.             THROW(kODErrWriteErr);
  389.     }
  390. }
  391.  
  392. //------------------------------------------------------------------------------
  393. // ODValueNameSpace: Write
  394. //------------------------------------------------------------------------------
  395.  
  396. SOM_Scope void  SOMLINK ODValueNameSpaceWrite(ODValueNameSpace *somSelf, Environment *ev,
  397.         PlatformFile* file, ODStorageUnitView* view)
  398. {
  399.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  400.     ODValueNameSpaceMethodDebug("ODValueNameSpace","Write");
  401.  
  402.     SOM_TRY
  403.     
  404.     // WRITE NAME
  405.     { TempODISOStr selfName(somSelf->GetName(ev));
  406.       WriteSizeAndBytes(file, view, 
  407.           (ODISOStr) selfName, ODISOStrLength( (ODISOStr) selfName ));
  408.     }
  409.     
  410.     // WRITE NUMENTRIES
  411.     ODULong numEntries = somSelf->GetNumEntries(ev);
  412.     WriteULong(file, view, numEntries);
  413.  
  414.     if (numEntries)
  415.     {
  416.         StringHashTableIterator    iter(somSelf->GetHashTable(ev));
  417.         ODUByte*                    key;
  418.         ODULong                    keyLength;
  419.         ODPtr                        value;
  420.         ODULong                    valueLength;
  421.         
  422.         for (iter.First(&key, &keyLength, &value, &valueLength)
  423.                 ;iter.IsNotComplete()
  424.                 ;iter.Next(&key, &keyLength, &value, &valueLength))
  425.         {
  426.             WriteSizeAndBytes(file, view, key, keyLength);
  427.             WriteSizeAndBytes(file, view, value, valueLength);
  428.         }
  429.     }
  430.     
  431.     SOM_CATCH_ALL
  432.     SOM_ENDTRY
  433. }
  434.  
  435. //------------------------------------------------------------------------------
  436. // ODValueNameSpace: WriteToFile
  437. //------------------------------------------------------------------------------
  438.  
  439. SOM_Scope void  SOMLINK ODValueNameSpaceWriteToFile(ODValueNameSpace *somSelf, Environment *ev,
  440.         ODByteArray* file)
  441. {
  442.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  443.     ODValueNameSpaceMethodDebug("ODValueNameSpace","WriteToFile");
  444.  
  445.     SOM_TRY
  446.  
  447.     PlatformFile*    platFile = ((PlatformFile*) file->_buffer);
  448.     somSelf->Write(ev, platFile, (ODStorageUnitView*)kODNULL);
  449.     
  450.     SOM_CATCH_ALL
  451.     SOM_ENDTRY
  452. }
  453.  
  454. //------------------------------------------------------------------------------
  455. // ODValueNameSpace: WriteToStorage
  456. //------------------------------------------------------------------------------
  457.  
  458. SOM_Scope void  SOMLINK ODValueNameSpaceWriteToStorage(ODValueNameSpace *somSelf, Environment *ev,
  459.         ODStorageUnitView* view)
  460. {
  461.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  462.     ODValueNameSpaceMethodDebug("ODValueNameSpace","WriteToStorage");
  463.  
  464.     SOM_TRY
  465.  
  466.     somSelf->Write(ev,0, view);
  467.     
  468.     SOM_CATCH_ALL
  469.     SOM_ENDTRY
  470. }
  471.  
  472. //------------------------------------------------------------------------------
  473. // ODValueNameSpace: Read
  474. //------------------------------------------------------------------------------
  475.  
  476. SOM_Scope void  SOMLINK ODValueNameSpaceRead(ODValueNameSpace *somSelf, Environment *ev,
  477.         PlatformFile* file, ODStorageUnitView* view)
  478. {
  479.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  480.     ODValueNameSpaceMethodDebug("ODValueNameSpace","Read");
  481.  
  482.     SOM_TRY
  483.  
  484.     {   // READ NAME-VERIFY THAT IT'S THE SAME
  485.         ODULong nameLength = ReadSize(file  , view);
  486.         TempODISOStr name = (ODISOStr)ODNewPtr(nameLength + 1, kDefaultHeapID);
  487.     
  488.         ReadBytes(file , view, name, &nameLength);
  489.         ((ODISOStr) name)[nameLength] = 0;
  490.     
  491.         TempODISOStr selfName(somSelf->GetName(ev)); // make sure it's deleted
  492.         
  493.         if ( ! ODISOStrEqual(name, selfName))
  494.             THROW(kODErrInvalidNSName);
  495.     }
  496.  
  497.     // READ NUM ENTRIES
  498.     ODULong numEntries = ReadSize(file , view);
  499.  
  500.     // MORE THAN ONE ENTRY?
  501.     if (numEntries)
  502.     {
  503.         ODISOStr    key = kODNULL;
  504.         ODPtr        value = kODNULL;
  505.         ODULong    keyLength;
  506.         ODULong    valueLength;
  507.         ODULong    bufferSize = UCHAR_MAX;
  508.         ODPtr        buffer = ODNewPtr(bufferSize, kDefaultHeapID); // FOR FASTER KEY READING
  509.         ODBoolean    allocatedKey = kODFalse;
  510.         ODVolatile(allocatedKey);
  511.         
  512.         ODNameSpaceManager* tempNSMgr = somSelf->GetNameSpaceManager(ev); //cache reference to speed loop
  513.         StringHashTable* tempHashTable = somSelf->GetHashTable(ev);
  514.         
  515.         TRY
  516.             for (ODULong i = 0; i < numEntries; i++)
  517.             {
  518.                 keyLength = ReadSize(file , view);
  519.                 if (keyLength > bufferSize)
  520.                 {
  521.                     key = (ODISOStr)ODNewPtr(keyLength, kDefaultHeapID);
  522.                     allocatedKey = kODTrue;
  523.                 }
  524.                 else
  525.                     key = (ODISOStr)buffer;
  526.  
  527.                 ReadBytes(file , view, key, &keyLength);
  528.  
  529.                 TRY
  530.                     valueLength = ReadSize(file , view);
  531.                     value = ODNewPtr(valueLength, kDefaultHeapID);
  532.                     TRY
  533.                         ReadBytes(file , view, value, 
  534.                                                                    &valueLength);
  535.                         tempHashTable->Insert((unsigned char*)key,
  536.                                                   keyLength, value, valueLength);
  537.  
  538.                         if (tempNSMgr->GetMaxISOStrLength(ev) < valueLength)
  539.                             tempNSMgr->SetMaxISOStrLength(ev, valueLength);
  540.                     
  541.                     CATCH_ALL
  542.                         ODDisposePtr(value);
  543.                         RERAISE;
  544.                     ENDTRY
  545.                 CATCH_ALL
  546.                     if (allocatedKey)
  547.                         ODDisposePtr(key);
  548.                     RERAISE;
  549.                 ENDTRY
  550.  
  551.                 if (allocatedKey)
  552.                     ODDisposePtr(key);
  553.             }
  554.             
  555.             ODDisposePtr(buffer);
  556.         CATCH_ALL
  557.             ODDisposePtr(buffer);
  558.             RERAISE;
  559.         ENDTRY
  560.     }
  561.     
  562.     SOM_CATCH_ALL
  563.     SOM_ENDTRY
  564. }
  565.  
  566. //------------------------------------------------------------------------------
  567. // ODValueNameSpace: ReadFromFile
  568. //------------------------------------------------------------------------------
  569.  
  570. SOM_Scope void  SOMLINK ODValueNameSpaceReadFromFile(ODValueNameSpace *somSelf, Environment *ev,
  571.         ODByteArray* file)
  572. {
  573.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  574.     ODValueNameSpaceMethodDebug("ODValueNameSpace","ReadFromFile");
  575.     
  576.     SOM_TRY
  577.     
  578.     PlatformFile*    platFile = ((PlatformFile*) file->_buffer);
  579.     somSelf->Read(ev, platFile, (ODStorageUnitView*)kODNULL);
  580.     
  581.     SOM_CATCH_ALL
  582.     SOM_ENDTRY
  583. }
  584.  
  585. //------------------------------------------------------------------------------
  586. // ODValueNameSpace: ReadFromStorage
  587. //------------------------------------------------------------------------------
  588.  
  589. SOM_Scope void  SOMLINK ODValueNameSpaceReadFromStorage(ODValueNameSpace *somSelf, Environment *ev,
  590.         ODStorageUnitView* view)
  591. {
  592.     /* ODValueNameSpaceData *somThis = ODValueNameSpaceGetData(somSelf); */
  593.     ODValueNameSpaceMethodDebug("ODValueNameSpace","ReadFromStorage");
  594.  
  595.     SOM_TRY
  596.  
  597.     somSelf->Read(ev,0, view);
  598.     
  599.     SOM_CATCH_ALL
  600.     SOM_ENDTRY
  601. }
  602.